package com.example.audiobooks.ranking.fragment;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.fastjson.JSON;
import com.example.audiobooks.R;
import com.example.audiobooks.base.BaseFragment;
import com.example.audiobooks.ranking.adapter.NewsAdapter;
import com.example.audiobooks.ranking.bean.ResultNewsBean;
import com.example.audiobooks.utils.Constants;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Response;

public class RankingFragment extends BaseFragment {
    private static final String TAG = RankingFragment.class.getSimpleName();
    private RecyclerView rvNews;
    private ResultNewsBean.ResultBean resultBean;
    private NewsAdapter adapter;


    @Override
    public View initView() {
        View view = View.inflate(mContext, R.layout.fragment_ranking, null);
        rvNews = (RecyclerView) view.findViewById(R.id.rv_news);

        getDataFromNet();  //联网请求数据；

        return view;
    }

    @Override
    public void initData() {
        super.initData();
    }

    private void getDataFromNet() {
        String url = Constants.NEWS_URL;
//        String url = "http://100.64.242.6:8080/zhubang/json/HOME_URL.json";    //数据请求成功；
        //使用真机测试时，需要连接在同一个网络，并且关闭防火墙
        OkHttpUtils
                .get()
                .url(url)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public String parseNetworkResponse(Response response, int id) throws IOException {
                        return super.parseNetworkResponse(response, id);
                    }

                    /*请求失败时回调*/
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        Log.e(TAG, "请求错误 " + e.getMessage());
                        AlertDialog.Builder dialog = new AlertDialog.Builder(getActivity());
                        dialog.setTitle("!!!");
                        dialog.setMessage("服务器请求失败，请查看网络连接");
                        dialog.setCancelable(false); //点击屏幕或返回键dialog不消失
                        dialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                            }
                        });
                        dialog.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                            }
                        });
                        dialog.show();
                    }

                    /*联网成功时回调*/
                    @Override
                    public void onResponse(String response, int id) {
                        Log.e(TAG, "竹帮学堂数据请求成功");
                        processData(response);//进行数据解析
                    }

                    private void processData(String json) {
                        ResultNewsBean resultNewsBean = JSON.parseObject(json, ResultNewsBean.class);
                        resultBean = resultNewsBean.getResult();
                        //测试解析用的
//                        Log.e(TAG, "解析数据 " + resultBean.getBanner_info().get(0).getOption());
                        if (resultBean != null) {
                            Log.e(TAG, "竹帮学堂有数据，进行数据解析中……");
                            //有数据；
                            //设置适配器；
                            adapter = new NewsAdapter(mContext, resultBean);
                            rvNews.setAdapter(adapter);
                            GridLayoutManager manager = new GridLayoutManager(mContext, 1);
                            //设置跨度大小的监听，用于回到顶部,当recommend模块显示时，进行显示；
                           /* manager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
                                @Override
                                public int getSpanSize(int position) {
                                    ib_top.setVisibility(View.VISIBLE);
//                                    if (position <= 2) {
//                                        ib_top.setVisibility(View.GONE);
//                                    } else {
//                                        ib_top.setVisibility(View.VISIBLE);
//                                    }
                                    //只能返回 1
                                    return 1;
                                }
                            });*/
                            //设置布局管理者
                            rvNews.setLayoutManager(manager);
                        } else {
                            //没有数据,弹出提示；
                            Log.e(TAG, "processData: 没有数据");
                        }
                    }
                });
    }

}
